<?php

/**
 * B端公共库文件
 * 主要定义B端公共函数库
 */

/*
 * url:访问路径
 * array:要传递的数组
 * */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}


   function curl_get($data,$url){


       $data = @http_build_query($data);  //把参数转换成URL数据
       $aContext = array('http' => array('method' => 'GET',

           'header'  => 'Content-type: application/x-www-form-urlencoded',

           'content' => $data ));

       $cxContext  = stream_context_create($aContext);

       $d = @file_get_contents($url,false,$cxContext);

       print_r($d);
   }
/*
 * url:访问路径
 * array:要传递的数组
 * */
    function curl_post($data,$url){


        $data = @http_build_query($data);  //把参数转换成URL数据

        $aContext = array('http' => array('method' => 'POST',

            'header'  => 'Content-type: application/x-www-form-urlencoded',

            'content' => $data ));

        $cxContext  = stream_context_create($aContext);


        $d = @file_get_contents($url,false,$cxContext);

         return $d;
    }




/**
 * 发送数据
 * @param String $url     请求的地址
 * @param Array  $header  自定义的header数据
 * @param Array  $content POST的数据
 * @return String
 */
function tocurl($url, $header, $content){
    $ch = curl_init();
    if(substr($url,0,5)=='https'){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
    $response = curl_exec($ch);
    if($error=curl_error($ch)){
        die($error);
    }
    curl_close($ch);
    return $response;
}


//function showPage($page,$totalPage,$where=null,$sep="&nbsp"){
//    $where=($where==null)?null:"&".$where;
//    $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//    $index=($page==1)?"首页":"<a href=".$url."?pages=1".$where.">首页</a>";
//    $last=($page==$totalPage)?"尾页":"<a href=".$url."?pages=".$totalPage.$where.">尾页</a>";
//    $prev=($page==1)?"上一页":"<a href=".$url."?pages=".($page-1).$where.">上一页</a>";
//    $next=($page==$totalPage)?"下一页":"<a href=".$url."?pages=".($page+1).$where.">下一页</a>";
//    $str="总".$totalPage."页/当前第".$page."页";
//
////循环显示页码
//    $p='';
//    for($i=1;$i<=$totalPage;$i++){
//        //当前页无链接
//        if($page===$i){
//            $p.="[".$i."]";
//        }else{
//            $p.="<a href=".$url."?pages=".$i.">"."[".$i."]"."</a>";
//        }
//    }
////    echo "<hr/>";
//    $pageStr = $index.$sep.$prev.$sep.$p.$sep.$next.$sep.$last.$sep.$str;
//    return $pageStr;
//}



/**
 * Ajax方式返回数据到客户端
 * @access protected
 * @param mixed $data 要返回的数据
 * @param String $type AJAX返回数据格式
 * @param int $json_option 传递给json_encode的option参数
 * @return void
 */
 function ajaxReturn($data,$type='',$json_option=0) {
    if(empty($type)) $type  =   C('DEFAULT_AJAX_RETURN');
    switch (strtoupper($type)){
        case 'JSON' :
            // 返回JSON数据格式到客户端 包含状态信息
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode($data,$json_option));
        case 'XML'  :
            // 返回xml格式数据
            header('Content-Type:text/xml; charset=utf-8');
            exit(xml_encode($data));
        case 'JSONP':
            // 返回JSON数据格式到客户端 包含状态信息
            header('Content-Type:application/json; charset=utf-8');
            $handler  =   isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
            exit($handler.'('.json_encode($data,$json_option).');');
        case 'EVAL' :
            // 返回可执行的js脚本
            header('Content-Type:text/html; charset=utf-8');
            exit($data);
        default     :
            // 用于扩展其他返回格式数据
            Hook::listen('ajax_return',$data);
    }
}


   function  headerParam($data,$header,$url){
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($header));
       $response = curl_exec($ch);
       curl_close($ch);


       print_r($response);
   }


   function checkLogin(){
       if($_SESSION['bAccessToken']==null){

           header('Location:http://'. $_SERVER['HTTP_HOST'].'/Admin/Admin/login');
       }
   }


//截取图片
  function crop($x,$y,$w,$h,$src,$filename){
    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;

//    $src = 'demo_files/pool.jpg';
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

    imagecopyresampled($dst_r,$img_r,0,0,$x,$y,
        $targ_w,$targ_h,$w,$h);

    header('Content-type: image/jpeg');
    imagejpeg($dst_r,$filename,$jpeg_quality);


}




//缩略图剪切
//$img 图片地址
//$url 保存路径
//$name图片保存名
function thum_images($img,$width,$height,$url,$name){
    //判断缩略图是否存在
    $goods_thumb_name =rand(1,999)."jiequ_{$width}_{$height}_{$name}";

    // 这个商品 已经生成过这个比例的图片就直接返回了
    if(file_exists($url.$goods_thumb_name.'.jpg'))  return './'.$url.$goods_thumb_name.'.jpg';
    if(file_exists($url.$goods_thumb_name.'.jpeg')) return './'.$url.$goods_thumb_name.'.jpeg';
    if(file_exists($url.$goods_thumb_name.'.gif'))  return './'.$url.$goods_thumb_name.'.gif';
    if(file_exists($url.$goods_thumb_name.'.png'))  return './'.$url.$goods_thumb_name.'.png';

    $image = new \Think\Image();
    $image->open($img);
    $goods_thumb_name = $goods_thumb_name. '.'.$image->type();     //$image->type()图片类型
    // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
    $image->thumb($width, $height)->save($url.$goods_thumb_name,NULL,100);
    return "./".$url.$goods_thumb_name;
}








//印章数据处理

function  doSealData($requestData,$requestLen){
    if($_REQUEST['sealCoordinate']){
        $array=explode(',', $_REQUEST['sealCoordinate']);
//                        print_r($array);exit;
//                        print_r(array_chunk($array, count($array)/$_REQUEST['sealCoordinateLen']));exit;
        $Arr=array_chunk($array, count($array)/$_REQUEST['sealCoordinateLen']);

        foreach ($Arr as $k=>$v){
            $Arr[$k]['left']= $Arr[$k][0];
            $Arr[$k]['top']= $Arr[$k][1];
            $Arr[$k]['width']= abs($Arr[$k][2]-$Arr[$k][0]);
            $Arr[$k]['height']= abs($Arr[$k][3]-$Arr[$k][1]);

        }

        return  $Arr;
//        print_r($Arr);exit;

    }
}


function showPage($page,$totalPage,$url,$pagesize,$totalData){
//    $url=$_SERVER['PHP_SELF'];

    //循环显示页码
    $p='';
    for($i=1;$i<=$totalPage;$i++){
        //当前页无链接
        if($page===$i){
//            $p.="[".$i."]";
            $p.="<a class='paginate_button previous disabled current' href=".$url."?pages=".$i.">".$i."</a>";
        }else{
            $p.="<a class='paginate_button previous disabled' href=".$url."?pages=".$i.">".$i."</a>";
        }
    }

    $start=($page-1)*$pagesize+1;
    $end=$pagesize*$page;
    if($end<$totalData){
        $end=$pagesize*$page;
    }else{
        $end=$totalData;
    }


    $cont='';
    $cont.='   <div class="page-wrap dataTables_wrapper">';
    $cont.='  <div class="dataTables_info" id="DataTables_Table_0_info" role="status" aria-live="polite">显示 '.$start.' 到 '.$end.' ，共 '.$totalData.' 条</div>';
    $cont.='<div class="dataTables_paginate paging_simple_numbers" id="DataTables_Table_0_paginate">';
    $cont.=($page==1)?"<a class='paginate_button previous disabled' aria-controls='DataTables_Table_0' data-dt-idx='0' tabindex='0' id='DataTables_Table_0_previous'>上一页</a>"  :"<a href=".$url."?pages=".($page-1).$where." class='paginate_button previous disabled' aria-controls='DataTables_Table_0' data-dt-idx='0' tabindex='0' id='DataTables_Table_0_previous'>上一页</a>";
    $cont.='<span>';
    $cont.="<a class=' ' aria-controls='DataTables_Table_0' data-dt-idx='1' tabindex='0'>".$p."</a>";
    $cont.='</span>';
    $cont.=($page==$totalPage)?"<a class='paginate_button next disabled' aria-controls='DataTables_Table_0' data-dt-idx='2' tabindex='0' id='DataTables_Table_0_next'>下一页</a>" :"<a   href=".$url."?pages=".($page+1).$where." class='paginate_button next disabled' aria-controls='DataTables_Table_0' data-dt-idx='2' tabindex='0' id='DataTables_Table_0_next'>下一页</a>" ;

    $cont.='</div>';
    $cont.='</div>';

    return $cont;

////    echo "<hr/>";
//    $pageStr = $index.$sep.$prev.$sep.$p.$sep.$next.$sep.$last.$sep.$str;
//    return $pageStr;
}

